#include <QPainter>
#include <QResizeEvent>

#include "ImageViewerWidget.h"

ImageViewerWidget::ImageViewerWidget(QWidget *parent) :
    QWidget(parent)
{
}

void ImageViewerWidget::paintEvent (QPaintEvent * event)
{
    // Get the painter and draw the image.
    QPainter painter(this);

    painter.drawImage(0, 0, m_scaledImage);

    QWidget::paintEvent(event);
}

void ImageViewerWidget::resizeEvent(QResizeEvent *event)
{
    update();
    emit signalSizeChanged(event->size());
}

void ImageViewerWidget::setImage(const QImage& image)
{
    m_image = image;
}

void ImageViewerWidget::slotPannerRectangleChanged(const QRect& rectangle)
{    
    if (!m_image.isNull())
    {
        m_scaledImage = m_image.copy(rectangle.x(), rectangle.y(), rectangle.width(), rectangle.height());
        m_scaledImage = m_scaledImage.scaledToWidth(width());
        update();
    }
}
